/**
 * 项目名：	Rumba Plan
 * 
 * 版权所有(C)，上海海鼎信息工程股份有限公司，2007，所有权利保留。
 * 
 * 文件名：	Interpretor.java
 * 模块说明：	
 * 修改历史：
 * Dec 6, 2007 - zhangyanbo - 创建。
 */
package com.hd123.rumba.qt.run;

import java.lang.reflect.Method;

import com.hd123.rumba.exception.BusinessException;
import com.hd123.rumba.qt.def.ISessionValues;
import com.hd123.rumba.util.StringUtil;

/**
 * @author zhangyanbo
 * 
 */
public class Interpretor {

  private ISessionValues sessionValues;

  public static Interpretor getInterpretor(ISessionValues sessionValues) throws BusinessException {
    Interpretor o = new Interpretor();
    o.sessionValues = sessionValues;
    return o;
  }

  private Interpretor() {
    super();
  }

  public String interpret(String clause) throws BusinessException {
    if (clause == null)
      return null;
    String result = clause;
    result = replaceAll(result, "\\(currentdomainuuid)", sessionValues.getCurrentDomain().getUuid());
    result = replaceAll(result, "\\(currentuseruuid)", sessionValues.getCurrentUser().getUuid());
    result = replaceSessionValues(result);
    return result;
  }

  private String replaceSessionValues(String clause) throws BusinessException {
    StringBuffer sb = new StringBuffer();
    int fromIndex = 0;
    int start = 0;
    int end = 0;
    do {
      start = clause.indexOf("\\(", fromIndex);
      if (start >= 0) {
        end = clause.indexOf(")", start);
        if (end >= 0) {
          sb.append(clause.substring(fromIndex, start));
          String substr = clause.substring(start + 2, end);
          String value = getSessionValue(substr);
          sb.append(value);
          fromIndex = end + 1;
        }
      } else
        sb.append(clause.substring(fromIndex));
    } while (start >= 0);
    return sb.toString();
  }

  private String getSessionValue(String path) throws BusinessException {
    Object[] keys = StringUtil.breakString(path, ".");
    Object obj = sessionValues.getSessionValue((String) keys[0]);
    if (obj == null)
      return "";
    Object value = obj;
    for (int i = 1; i < keys.length; i++) {
      String key = (String) keys[i];
      if (key.length() <= 0)
        break;
      String functionName = "get" + key.substring(0, 1).toUpperCase() + key.substring(1);
      try {
        Method m = value.getClass().getMethod(functionName, (Class[]) null);
        Object v = m.invoke(value, (Object[]) null);
        if (v == null)
          return "";
        value = v;
      } catch (Exception e) {
        throw new BusinessException("从 " + path + " 获得对象属性值出错。", e);
      }
    }

    if (value != null) {
      if (value instanceof String)
        return (String) value;
      else
        return value.toString();
    } else
      return "";
  }

  private String replaceAll(String sql, String substr, String replacedStr) throws BusinessException {
    if (sql == null)
      return "";
    if (replacedStr == null)
      return sql;
    String result = sql;
    int index = result.toLowerCase().indexOf(substr.toLowerCase());
    while (index >= 0) {
      result = result.substring(0, index) + replacedStr + result.substring(index + substr.length());
      index = result.toLowerCase().indexOf(substr.toLowerCase());
    }
    return result;
  }

}
